<?php
/* 
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * $Rev: 44 $
 * $Id: class.app.php 44 2011-09-20 09:27:40Z superbounou $
 * $Author: superbounou $
 * $Date: 2011-09-20 09:27:40 +0000 (Tue, 20 Sep 2011) $
 * 
 * Control app pages
 */
class App {

	private static $debug;

	public static $app;

	/**
	 * List of template
	 */
	private $tpl = array ();

	/**
	 * Constructor
	 */
	public function __construct() {
		$conffile = 'conf.php';
		assert('/*checking conf file existence*/file_exists($conffile)');
		/* include the active app conf file */
		include($conffile);
		$this->tpl = scandir('tpl');
		array_shift($this->tpl);
		array_shift($this->tpl);
		assert('/*testing if templates are present*/count($this->tpl) > 2');
		/* debug */
		if (ACTIVE_DEBUG) {
			self :: $debug['mysql'] = 'MySQLData';
			self :: $debug['include'] = 'App';
		}
	}

	public static function addDebug($section, $debugger) {
		assert('method_exists($debugger, \'debug\')');
		self :: $debug[$section] = $debugger;
	}

	public static function debug() {
		Helper :: displayDebug(get_included_files());
	}

	/**
	 * Generate the output
	 */
	public function output() {
		if (!isset ($_GET['action'])) {
			header('Location: index.php?action=' . DEFAULT_ACTION . '.index');
		}
		assert('/*testing called action method*/isset ($_GET[\'action\']) && strpos($_GET[\'action\'], \'.\') !== false');
		$action = preg_split('/[.]/', $_GET['action']);
		assert('/*testing the syntax method*/preg_match(\'/[a-zA-Z]+/\', $action[0]) && preg_match(\'/[a-zA-Z]+/\', $action[1])');
		$class = $action[0];
		$method = $action[1];
		$classfile = 'action' . DIRECTORY_SEPARATOR . 'action.' . strtolower($class) . '.php';
		assert('/*testing if the action file (' . $classfile . ') exists*/file_exists($classfile)');
		include ($classfile);
		$class = $class . 'Action';
		assert('/*testing if the action class is defined*/class_exists($class)');
		$action = new $class ($class);
		unset ($_GET['action']);
		assert('/*the method "' . $method . '" does not exist*/method_exists($action, $method)');
		header('Content-type: text/html; charset=UTF-8'); 
		$action-> $method ($_GET, $_POST);
		if (ACTIVE_DEBUG) {
			foreach (self :: $debug as $section => $helper) {
				echo '<pre class="debug-lyna"><h1>' . $section . '</h1><ul>';
				call_user_func(array (
					$helper,
					'debug'
				));
				echo '</ul></pre>';
			}
		}
	}
}
